package com.luch.controller;

import com.luch.domain.Result;
import com.luch.domain.User;
import com.luch.eaums.ResultEnum;
import com.luch.respository.UserRespository;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * Creator lh on 2017/8/15 14:21.
 * Email:luchefg@gmail.com
 * Description:
 */
@CrossOrigin
@RestController
public class LoginController extends BaseController {

    private UserRespository userRespository;

    @ApiOperation(value = "登录", notes = "用户名+密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "用户名", required = true, dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "code", value = "验证码", required = true, dataType = "String", paramType = "body"),

    })
    @PostMapping("/login")
    public Result Login(@Valid String name, String password, String code) {
        if (name == null) {
            return new Result(ResultEnum.PARAMS_ERROR);
        } else {
            List<User> list = new ArrayList<>();
            list = userRespository.findAllByName(name);
            if (list.size() == 0) {
                return new Result(ResultEnum.PARAMS_ERROR);
            } else {
                return new Result(ResultEnum.SUCCESSFUL, list);
            }
        }
    }

}
